
package com.gitee.jmash.shiro;

import java.util.Base64;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;

/** Shiro Cookie Key. */
public class CookieKey {

  @Test
  public void createKey() {
    // AES key
    String key = Base64.getEncoder().encodeToString(getRandom(16).getBytes());
    LogFactory.getLog(CookieKey.class).info(key);
  }

  /** Random String. */
  public String getRandom(int length) {
    char[] arr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
        'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
        'y', 'z' };
    String result = String.valueOf(arr[(int) Math.floor(Math.random() * 36)]);
    for (int i = 1; i < length; i++) {
      result += arr[(int) Math.floor(Math.random() * 36)];
    }
    return result;
  }

}
